<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:mo="https://gitee.com/aun/Timo">
<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})">
    <meta name="viewport" content="initial-scale=1, minimum-scale=1, width=device-width">
    <meta name="twitter:widgets:csp" content="on">
    <link rel="stylesheet" data-name="vs/editor/editor.main" href="/js/plugins/monaco/min/vs/editor/editor.main.css">
</head>
<body>
    <div class="layui-form timo-compile">
    <form th:action="@{/system/schedule/save}">
        <input type="hidden" name="id" th:if="${schedule}" th:value="${schedule.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">定时计划标题</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" name="title" placeholder="请输入定时计划标题" th:value="${schedule?.title}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">cron定时表达式(<a href="http://cron.ciding.cc/" target="_blank">生成器</a> )</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" name="cron" placeholder="请输入cron定时表达式" th:value="${schedule?.cron}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任务要执行的代码（java1.4规范）</label>
            <div class="layui-input-inline">
                <textarea style="display: none;" rows="3" cols="100" name="execcode" th:utext="${schedule?.execcode}"></textarea>
                <div style="height:350px;" id="monaco-editor"></div>
                <div><button class="layui-btn btn-run"> 运行一下</button></div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark">[[${schedule?.remark}]]</textarea>
            </div>
        </div>
        <div class="layui-form-item timo-finally">
            <button class="layui-btn btn-submit"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary close-popup"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script th:replace="/common/template :: script"></script>
<script type="text/javascript" src="/js/plugins//monaco/jquery-1.12.2.min.js"></script>
<script>var require = {paths: {'vs': '/js/plugins/monaco/min/vs'}};</script>
<script src="/js/plugins//monaco/min/vs/loader.js"></script>
<script src="/js/plugins//monaco/min/vs/editor/editor.main.nls.js"></script>
<script src="/js/plugins//monaco/min/vs/editor/editor.main.js"></script>
<script src="/js/plugins//monaco/min/vs/custom-language.js?v=1.0"></script>
<script src="/js/plugins//monaco/min/vs/basic-languages/yaml/yaml.js"></script>
<script type="text/javascript" src="/js/plugins//monaco/ide_boot.js?v=1.0.1"></script>
<script>
    $(document).ready(function () {
        //将原来的内容放进去
        editorTextarea.setValue($('[name="execcode"]').text());

        $(document).on("click", ".btn-submit", function (e) {
            e.preventDefault();
            $('[name="execcode"]').text(editorTextarea.getValue());
            var form = $(this).parents("form");
            var url = form.attr("action");
            var serializeArray = form.serializeArray();
            $.post(url, serializeArray, function (result) {
                if (result.data == null) {
                    result.data = 'submit[refresh]';
                }
                $.fn.Messager(result);
            });
        });

        $(document).on("click", ".btn-run", function (e) {//运行一下
            e.preventDefault();
            $('[name="execcode"]').text(editorTextarea.getValue());
            var form = $(this).parents("form");
            var url = "/system/schedule/runscript";
            var serializeArray = form.serializeArray();
            console.log(serializeArray);
            $.post(url, serializeArray, function (result) {
                let msg = "";
                if(result.code==200){
                    msg =("运行结果：\n"+result.msg+"<br>----------------<br>"+JSON.stringify(result.data));
                }else{
                    msg = result.msg;
                }
                layer.alert(msg, {area: ['80%', '60%'],offset: '15px', icon: 1,btn:['知道了']},function (index) {
                    layer.close(index);
                });
            });
        });
    });
</script>
</body>
</html>